package og.example.dao;
import og.example.entity.Class;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;

public class ClassDataProvider {
    private static AtomicInteger classIdGenerator = new AtomicInteger(0);
    private static List<Class> classes = new CopyOnWriteArrayList<>();

    public static boolean classAdd(String className) {
        for (Class cls : classes) {
            if (cls.getClassName().equals(className)) {
                return false; // 班级已存在
            }
        }
        Class newClass = new Class(classIdGenerator.incrementAndGet(), className, 0);
        classes.add(newClass);
        return true;
    }

    public static boolean classDelete(String className) {
        return classes.removeIf(cls -> cls.getClassName().equals(className));
    }

    public static boolean classUpdate(String className, String newClassName) {
        for (Class cls : classes) {
            if (cls.getClassName().equals(className)) {
                cls.setClassName(newClassName);
                return true;
            }
        }
        return false;
    }

    public static List<Class> classQuery() {
        return new ArrayList<>(classes);
    }

    public static void incrementGroupCountForClass(String className) {
        for (Class cls : classes) {
            if (cls.getClassName().equals(className)) {
                cls.setGroupCount(cls.getGroupCount() + 1);
                break;
            }
        }
    }

    public static void decrementGroupCountForClass(String className) {
        for (Class cls : classes) {
            if (cls.getClassName().equals(className)) {
                int currentCount = cls.getGroupCount();
                if (currentCount > 0) {
                    cls.setGroupCount(currentCount - 1);
                }
                break;
            }
        }
    }
}